/*  LinePrinterBridge.java Version 101 - 3rd April 2008
 *  LinePrinterBridge.java communicates with an old dot matrix printer
 *  See Versions for program version number
 *  Copyright (C) 2008  Andy Broomfield andy@newmediathinking.com
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
*/

package net.blogbotplatform.blogbot;

import gnu.io.*;

import java.io.IOException;
import java.io.OutputStream;
import java.util.Enumeration;

//sun.io.CharToByteConverter need replacing???
import sun.io.CharToByteConverter;

public class LinePrinterBridge {
	
//	static int printerInterval = 100;
//	static long printerTimer;

	static OutputStream outputStream;;
	static ParallelPort parallelPort;
	static CommPortIdentifier port;

	static CommPortIdentifier portId;
	static Enumeration portList;
	
	boolean printerAvalible = false;

	//static String printerPort;

	static final String[] PORT_TYPE = { "Serial Port", "Parallel Port" };

	public LinePrinterBridge() {
	}
	
	public void openLinePrinterPort(String printerPort) {
		try {
			port = CommPortIdentifier.getPortIdentifier(printerPort);
			parallelPort = (ParallelPort) port.open("LinePrinter", 50);
			printerAvalible = true;
		} 
		catch (NoSuchPortException nspe) {
			System.out.println("NoSuchPortException : " + nspe);
		} 
		catch (PortInUseException piue) {
			System.out.println("PortInUseException. : " + piue);
		}
	}
	
	/*these are simple functions for sending commands to the printer
	 * first by setting up a simple print line function
	 * then follows with simple functions which can be expanded to send
	 * printer codes as needed, all through the sendPrint function
	 */
	
	//sends one line to the old dot matrix printer, max line is 80 chars, does not add line feed
	void sendPrint(String whatToPrint) {
		
		if (printerAvalible == false) return;

		try {
			// open the parallel port
			outputStream = parallelPort.getOutputStream();

			char[] charArray = whatToPrint.toCharArray();
			byte[] byteArray = CharToByteConverter.getConverter("UTF8").convertAll(charArray);
			outputStream.write(byteArray);
			outputStream.flush();
			System.out.println("Done Printing.");
			outputStream.close();

		} 
/*		catch (PortInUseException piue) {
			System.out.println("PortInUseException. : " + piue);
		} 
*/
		catch (IOException ioe) {
			System.out.println("IOException : " + ioe);

		} 
		catch (Exception e) {
			System.out.println("Failed to print : " + e);

		} 
/*		finally {
			if (port != null && port.isCurrentlyOwned()) {
				parallelPort.close();
			}

			System.out.println("Closed Printer Port, done printing");
		}
*/
	}
	
	//like println, now we can print a line feed afterwards
	void sendPrintln(String whatToPrint) {
		whatToPrint += "\n";
		sendPrint(whatToPrint);
	}
	
	//a simple carrige return can be used as a line feed
	void lineFeed() {
		sendPrint("\n");
	}
	
	//line feed the paper so the last print out is just above the tear off bar (readable)
	void spitOut() {
		sendPrint("\n\n\n\n\n");	
	}
	
	void closeLinePrinterPort() {
		System.out.println("Closing Port");
		printerAvalible = false;
		try {
			parallelPort.close();
		}
		catch (Exception e) {
			System.out.println("Failed to Close Printer Port : " + e);
		} 
	}

}



